package com.liuhongwei.test.builder;

import com.liuhongwei.test.bean.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.util.List;

/**
 * @program: workspace-easyjava
 * @description:
 * @author: cw
 * @create: 2024-05-11 10:19
 **/
public class BuildBase {

    private static Logger log = LoggerFactory.getLogger(BuildBase.class);
    private static List<String> imports;

    public static void execute() {
        build(Constants.PACKAGE_ENUMS, "DateTimePatternEnum", Constants.PATH_ENUMS, null);
        build(Constants.PACKAGE_ENUMS, "PageSize", Constants.PATH_ENUMS, null);
        build(Constants.PACKAGE_ENUMS, "ResponseCodeEnum", Constants.PATH_ENUMS, null);

        build(Constants.PACKAGE_UTILS, "DateUtil", Constants.PATH_UTILS, null);
        build(Constants.PACKAGE_MAPPERS, "BaseMapper", Constants.PATH_MAPPERS, null);

        build(Constants.PACKAGE_QUERY, "BaseQuery", Constants.PATH_QUERY, null);
        imports = Arrays.asList(Constants.PACKAGE_ENUMS + ".PageSize");
        build(Constants.PACKAGE_QUERY, "SimplePage", Constants.PATH_QUERY, imports);

        build(Constants.PACKAGE_VO, "PaginationResultVO", Constants.PATH_VO, null);
        build(Constants.PACKAGE_VO, "ResponseVO", Constants.PATH_VO, null);
        imports = Arrays.asList(Constants.PACKAGE_VO + ".ResponseVO",
                Constants.PACKAGE_ENUMS + ".ResponseCodeEnum");
        build(Constants.PACKAGE_CONTROLLER, "BaseController", Constants.PATH_CONTROLLER, imports);

        imports = Arrays.asList(Constants.PACKAGE_ENUMS + ".ResponseCodeEnum");
        build(Constants.PACKAGE_EXCEPTION, "BusinessException", Constants.PATH_EXCEPTION, imports);
        imports = Arrays.asList(Constants.PACKAGE_VO + ".ResponseVO",
                Constants.PACKAGE_ENUMS + ".ResponseCodeEnum",
                Constants.PACKAGE_CONTROLLER + ".BaseController");
        build(Constants.PACKAGE_EXCEPTION, "GlobalExceptionHandler", Constants.PATH_EXCEPTION, imports);
    }

    public static void build(String packagePath, String fileName, String outputPath, List<String> imports) {
        File folder = new File(outputPath);
        if (!folder.exists()) {
            folder.mkdirs();
        }

        File file = new File(outputPath, fileName + ".java");
        OutputStream os = null;
        OutputStreamWriter osw = null;
        BufferedWriter bw = null;

        InputStream is = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        try {
            os = new FileOutputStream(file);
            osw = new OutputStreamWriter(os, "UTF-8");
            bw = new BufferedWriter(osw);

            is = BuildBase.class.getResourceAsStream("/template/" + fileName + ".txt");
            isr = new InputStreamReader(is, "UTF-8");
            br = new BufferedReader(isr);

            bw.write("package " + packagePath + ";\n\n");
            if (imports != null && imports.size() > 0) {
                for (String imp : imports) {
                    bw.write("import " + imp + ";\n");
                }
                bw.newLine();
            }
            String line;
            while ((line = br.readLine()) != null) {
                bw.write(line + "\n");
            }
            bw.flush();
        } catch (Exception e) {
            log.error("生成基础类：{}，失败", fileName, e);
        } finally {
            close(os, osw, bw, is, isr, br);
        }
    }

    private static void close(OutputStream os, OutputStreamWriter osw, BufferedWriter bw, InputStream is,
            InputStreamReader isr, BufferedReader br) {
        if (br != null) {
            try {
                br.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (isr != null) {
            try {
                isr.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (is != null) {
            try {
                is.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        close(os, osw, bw);
    }

    public static void close(OutputStream os, OutputStreamWriter osw, BufferedWriter bw) {
        if (bw != null) {
            try {
                bw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (osw != null) {
            try {
                osw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (os != null) {
            try {
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        BuildBase.execute();
    }

}
